<?php
class financeController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "finance";

    public function order_award_detail()
    {
        $priv = array($this->_pvprefix . "_oa");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $data = [];

            $this->view->auth = $auth;

            if (isset($_GET['start_time']) && $_GET['start_time'] != "") {
                $para['start_time'] = addslashes($_GET['start_time']);

                $this->view->start_time = $para['start_time'];
            } else {
                $this->view->start_time = date("Y-m-d 0:0:0");
            }

            if (isset($_GET['end_time']) && $_GET['end_time'] != "") {
                $para['end_time'] = addslashes($_GET['end_time']);

                $this->view->end_time = $para['end_time'];
            } else {
                $this->view->end_time = date("Y-m-d 23:59:59");
            }

            if (isset($_GET['caid']) && $_GET['caid'] != "") {
                $para['caid'] = intval($_GET['caid']);
            }

            if ($para['caid'] > 0 && $para['start_time']!="" && $para['end_time']!="") {

                $data = $model->getOrderAwardDetail($para);
            }

            //发放权限
            $this->view->pv['as'] = $model->checkPriv($this->_pvprefix . "_as", false);
            $this->view->cate     = $model->getCate();
            $this->view->data     = $data;
            $this->display();

        }
    }

/*会员相关*/

    //待入账记录
    public function drz()
    {

        $priv = array($this->_pvprefix . "_drz");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            $this->view->auth = $auth;

            if (isset($_GET['type']) && $_GET['type'] != "") {

                $type = intval($_GET['type']);
                if ($type > 0) {
                    $para['type'] = $type;
                }

            }

            if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }

            if (intval($_GET['uid'])) {

                $uid = intval($_GET['uid']);
                if ($uid > 0) {
                    $para['uid'] = $uid;
                }

            }

            if ($_GET['start_time'] != "") {
                $start_time = strtotime($_GET['start_time'] . " 0:0:0");
                if ($start_time > 0) {
                    $para['start_time'] = $start_time;
                }
            }

            if ($_GET['end_time'] != "") {
                $end_time = strtotime($_GET['end_time'] . " 23:59:59");
                if ($end_time > 0) {
                    $para['end_time'] = $end_time;
                }
            }

            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }

            $this->view->data = $model->getDRZ($para, $page);

            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }

            $this->display();

        }

    }

    //会员收支明细
    public function finance_detail()
    {

        $priv = array($this->_pvprefix . "_dt");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            $this->view->auth = $auth;

            if (isset($_GET['event']) && $_GET['event'] != "") {

                $event = intval($_GET['event']);
                if ($event > 0) {
                    $para['event'] = $event;
                }

            }

            if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }

            if (intval($_GET['uid'])) {

                $uid = intval($_GET['uid']);
                if ($uid > 0) {
                    $para['uid'] = $uid;
                }

            }

            if ($_GET['start_time'] != "") {
                $start_time = strtotime($_GET['start_time'] . " 0:0:0");
                if ($start_time > 0) {
                    $para['start_time'] = $start_time;
                }
            }

            if ($_GET['end_time'] != "") {
                $end_time = strtotime($_GET['end_time'] . " 23:59:59");
                if ($end_time > 0) {
                    $para['end_time'] = $end_time;
                }
            }

            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }

            $this->view->data = $model->getFinanceDetail($para, $page);
            //导出权限
            $this->view->pv['export'] = $model->checkPriv($this->_pvprefix . "_dex", false);
            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }

            $this->display();

        }

    }

    //打印页面
    public function withdrawprint()
    {
        $priv = array($this->_pvprefix . "_wp");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            $order_id = intval($_GET['order_id']);
            $data     = $model->getWithdrawPrintDetail($order_id);
            //print_r($data);die;
            $this->view->data = $data;
            $this->view->auth = $auth;
            $this->display();
        }
    }

    // 导出提现信息
    public function withdraw_all_export()
    {
        $priv = array($this->_pvprefix . "_wt");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);

        if ($auth) {

            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if ($_GET['money'] != "") {
                $money = intval($_GET['money']);
                if ($money != '') {
                    $para['money'] = $money;
                }

            }

            if ($_GET['status'] != "") {
                $status = intval($_GET['status']);
                if ($status >= 0) {
                    $para['status'] = $status;
                }

            }

            if ($_GET['withdraw_type'] != "") {
                $withdraw_type         = intval($_GET['withdraw_type']);
                $para['withdraw_type'] = $withdraw_type;

            }
            $data = $model->getWithdrowData($para);
            //PRINT_R($data);DIE;
            if ($data) {
                foreach ($data as $k => $v) {
                    $orderData[] = array(
                        'id'            => $v['id'],
                        'uid'           => "[" . $v['uid'] . "]" . $v['user_name'],
                        'create_time'   => date('Y-m-d H:i', $v['create_time']),
                        'money'         => $v['money'],
                        'charge_fee'    => $v['charge_fee'],
                        'realname'      => $v['realname'],
                        'mobile'        => $v['mobile'],
                        'bank_sn'       => $v['bank_sn'],
                        'bank_account'  => $v['bank_account'],
                        'stat'          => $this->c['stat']['withdraw_stat'][$v['stat']],
                        'withdraw_type' => $this->c['stat']['withdraw_type'][$v['withdraw_type']],

                    );
                }

            }

            RPHP::loadClass("PHPExcel", "extend/PHPExcel/Classes");
            $excel       = new PHPExcel();
            $letter      = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'); //,'N'   快递费用
            $tableheader = array('流水号', '用户昵称', '申请时间', '提现金额', '手续费', '收款人', '收款人电话', '开户行', '银行账户', '状态', '类型');
            $excel_H     = 2;

            $excel->getActiveSheet()->getColumnDimension("A")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("B")->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension("C")->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension("D")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("E")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("F")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("G")->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension("H")->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension("I")->setWidth(40);
            $excel->getActiveSheet()->getColumnDimension("J")->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension("K")->setWidth(15);

            for ($i = 0; $i < count($tableheader); $i++) {

                $excel->getActiveSheet()->setCellValue($letter[$i] . "1", $tableheader[$i]);
            }
            if ($orderData) {
                foreach ($orderData as $k => $v) {
                    $line = $excel_H + $k;
                    $excel->getActiveSheet()->setCellValue('A' . $line, $v['id']);
                    $excel->getActiveSheet()->setCellValue('B' . $line, $v['uid']);
                    $excel->getActiveSheet()->setCellValue('C' . $line, $v['create_time']);
                    $excel->getActiveSheet()->setCellValue('D' . $line, $v['money']);
                    $excel->getActiveSheet()->setCellValue('E' . $line, $v['charge_fee']);
                    $excel->getActiveSheet()->setCellValue('F' . $line, $v['realname']);
                    $excel->getActiveSheet()->setCellValueExplicit('G' . $line, $v['mobile'] . ' ');
                    $excel->getActiveSheet()->setCellValue('H' . $line, stripcslashes($v['bank_sn']));
                    $excel->getActiveSheet()->setCellValue('I' . $line, $v['bank_account'] . ' ');
                    $excel->getActiveSheet()->setCellValue('J' . $line, $v['stat']);
                    $excel->getActiveSheet()->setCellValue('K' . $line, $v['withdraw_type']);
                }
            }
            //创建Excel输入对象
            $write = new PHPExcel_Writer_Excel5($excel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/vnd.ms-execl");
            header('Content-Disposition:attachment;filename="提现记录' . date("Y-m-d_H:i:s", $_SERVER['REQUEST_TIME']) . '.xls"');
            header("Content-Transfer-Encoding:binary");
            $write->save('php://output');
        }

    }

    // 导出等待处理提现信息
    public function withdraw_export()
    {
        $priv = array($this->_pvprefix . "_wt");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $data = $model->getUntreatedData();

            RPHP::loadClass("PHPExcel", "extend/PHPExcel/Classes");
            $excel       = new PHPExcel();
            $letter      = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'); //,'N'   快递费用
            $tableheader = array('流水号', '申请时间', '金额', '收款人', '收款人电话', '开户行', '银行账户', ' 备注信息');
            $excel_H     = 2;

            $excel->getActiveSheet()->getColumnDimension("A")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("B")->setWidth(22);
            $excel->getActiveSheet()->getColumnDimension("C")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("D")->setWidth(12);
            $excel->getActiveSheet()->getColumnDimension("E")->setWidth(22);
            $excel->getActiveSheet()->getColumnDimension("F")->setWidth(42);
            $excel->getActiveSheet()->getColumnDimension("G")->setWidth(28);
            $excel->getActiveSheet()->getColumnDimension("H")->setWidth(40);

            for ($i = 0; $i < count($tableheader); $i++) {

                $excel->getActiveSheet()->setCellValue($letter[$i] . "1", $tableheader[$i]);
            }
            if ($data) {
                foreach ($data as $k => $v) {
                    $line = $excel_H + $k;
                    $excel->getActiveSheet()->setCellValue('A' . $line, $v['id']);
                    $excel->getActiveSheet()->setCellValue('B' . $line, date("Y-m-d H:i:s", $v['create_time']));
                    $excel->getActiveSheet()->setCellValue('C' . $line, $v['money']);
                    $excel->getActiveSheet()->setCellValue('D' . $line, stripcslashes($v['realname']));
                    $excel->getActiveSheet()->setCellValue('E' . $line, stripcslashes($v['mobile']));
                    $excel->getActiveSheet()->setCellValue('F' . $line, stripcslashes($v['bank_sn']));
                    $excel->getActiveSheet()->setCellValueExplicit('G' . $line, $v['bank_account'], PHPExcel_Cell_DataType::TYPE_STRING);
                    $excel->getActiveSheet()->setCellValue('H' . $line, stripcslashes($v['note']));
                }
            }
            //创建Excel输入对象
            $write = new PHPExcel_Writer_Excel5($excel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/vnd.ms-execl");
            header('Content-Disposition:attachment;filename="等待处理的提现.xls"');
            header("Content-Transfer-Encoding:binary");
            $write->save('php://output');
        }
    }

    //提现列表
    public function withdraw()
    {

        $priv = array($this->_pvprefix . "_wt");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if ($_GET['money'] != "") {
                $money = intval($_GET['money']);
                if ($money != '') {
                    $para['money'] = $money;
                }

            }

            if ($_GET['status'] != "") {
                $status = intval($_GET['status']);
                if ($status >= 0) {
                    $para['status'] = $status;
                }

            }

            if ($_GET['withdraw_type'] != "") {
                $withdraw_type         = intval($_GET['withdraw_type']);
                $para['withdraw_type'] = $withdraw_type;

            }

            if ($_GET['coin_type'] != "") {
                $coin_type         = intval($_GET['coin_type']);
                $para['coin_type'] = $coin_type;

            }

            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }

            $data = $model->getWithdrawList($para, $page);
            //获取页码
            if ($data['maxpage'] > 1) {
                $data['spage'] = $model->fun->spage($data['page'], $data['maxpage']);
            }

            $this->view->data = $data;
            $this->display();

        }

    }

    //提现详细信息
    public function withdraw_detail()
    {
        $priv = array($this->_pvprefix . "_wt");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $id = intval($_GET['id']);
            if ($id > 0) {
                $data = $model->getSingleWithdraw($id);
            }

            if ($data) {
                $this->view->auth  = $auth;
                $this->view->appid = $model->getWeiXinSetting();
                $this->view->data  = $data;

                $this->view->pv['execute'] = $model->checkPriv($this->_pvprefix . "_wtex", false);

                $this->display();

            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //保存提现信息
    public function savewithdraw()
    {
        $priv = array($this->_pvprefix . "_wtex");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $code             = 900;

            if ($_POST['act'] != "") {

                $this->view->success = "保存提现信息成功";
                $this->view->link    = array(
                    array('name' => '返回来源页面', 'url' => $_POST['ref']),
                    array('name' => '返回提现管理', 'url' => '/finance/withdraw'),
                );
                $code = $model->saveWithdraw($_POST);

            }

            $this->view->code = $code;
            $this->displaydo();

        }
    }

    //AJAX
    public function ajax()
    {
        $priv = array($this->_pvprefix . "_ls");

        $model = $this->model;
        $auth  = $model->checkPriv($priv, false);
        if ($auth) {
            if ($_POST['act'] == "totaldetail") {
                $money = $model->totalDetail($_POST);
                echo $money;
            } elseif ($_POST['act'] == 'ajax_since') {
                //自提
                $data = $model->saveSince($_POST);
                echo json_encode($data);
            } elseif ($_POST['act'] == 'award_send' && $model->checkPriv($this->_pvprefix . "_as", false, false)) {
                //发放销售奖励
                // print_r($_POST);die;
                $data = $model->sendOrderAwardMoney($_POST);
                echo json_encode($data);
            }

        }
    }

    //导入界面
    public function import()
    {
        $priv = array($this->_pvprefix . "_mim");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->display();
        }
    }

    //导出操作
    public function exportBatch()
    {
        $priv = array($this->_pvprefix . "_mex");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            if ($_GET['start_time'] != "" && $_GET['end_time'] != "") {
                $start_time = strtotime($_GET['start_time'] . ":0");
                $end_time   = strtotime($_GET['end_time'] . ":0 -1 sec");
                $data       = $model->getMemberPayinfoData($start_time, $end_time, $supplier_id, $warehouse_type, $type);

                if ($data['member_payinfo']) {
                    $region = $model->getRegion();
                    foreach ($data['member_payinfo'] as $k => $v) {
                        $orderData[] = array(
                            'order_sn'     => $v['order_sn'],
                            'uid'          => $v['uid'],
                            'realname'     => $data['user'][$v['uid']]['realname'],
                            'tao_mobile'   => $v['tao_mobile'],
                            'tao_name'     => $v['tao_name'],
                            'address'      => $region[$v['tao_province_id']] . $region[$v['tao_city_id']] . $region[$v['tao_county_id']] . $v['tao_address'],
                            'money'        => $v['money'],
                            'level_money'  => $v['level_money'],
                            'order_type'   => $v['order_type'],
                            'tao_info'     => $v['tao_info'],
                            'express_id'   => $v['express_id'],
                            'express_name' => $v['express_name'],

                            'ctime'        => date("Y-m-d H:i:s", $v['ctime']),

                        );
                    }

                }

                if ($orderData) {
                    RPHP::loadClass("PHPExcel", "extend/PHPExcel/Classes");
                    $excel = new PHPExcel();

                    //设置excel属性基本信息
                    $excel->getProperties()->setCreator("YULINJUN")
                        ->setLastModifiedBy("YULINJUN")
                        ->setTitle("YULINJUN")
                        ->setSubject("会员开通记录")
                        ->setDescription("")
                        ->setKeywords("会员开通记录")
                        ->setCategory("");
                    $excel->setActiveSheetIndex(0);
                    $excel->getActiveSheet()->setTitle("会员开通记录");
                    //填入表头主标题
                    $excel->getActiveSheet()->setCellValue('A1', '会员开通记录');
                    //填入表头副标题
                    $excel->getActiveSheet()->setCellValue('A2', '导出时间范围：' . date("Y-m-d H:i:s", $start_time) . '至' . date("Y-m-d H:i:s", $end_time));
                    //合并表头单元格
                    $excel->getActiveSheet()->mergeCells('A1:M1');
                    $excel->getActiveSheet()->mergeCells('A2:M2');

                    //设置表头行高
                    $excel->getActiveSheet()->getRowDimension(1)->setRowHeight(40);
                    $excel->getActiveSheet()->getRowDimension(2)->setRowHeight(20);
                    $excel->getActiveSheet()->getRowDimension(3)->setRowHeight(30);

                    //设置表头字体
                    $excel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
                    $excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
                    $excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
                    $excel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
                    $excel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
                    $excel->getActiveSheet()->getStyle('A3:M3')->getFont()->setBold(true);
                    $letter      = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q');
                    $tableheader = array('订单号', '用户UID', '用户昵称', '收货人手机号码', '收货姓名', '收货地址', '支付金额', '级别金额', '订单类型', '套餐包', '快递单号', '快递公司', '开通时间');
                    $excel_H     = 4;
                    for ($i = 0; $i < count($tableheader); $i++) {
                        $excel->getActiveSheet()->setCellValue("$letter[$i]3", "$tableheader[$i]");
                    }

                    $excel->getActiveSheet()->getStyle('A')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('B')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('D')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('F')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('M')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                    $excel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
                    $excel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('D')->setWidth(16);
                    $excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('F')->setWidth(80);
                    $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('L')->setWidth(15);
                    $total_money = 0;
                    foreach ($orderData as $num => $v) {
                        $line = $num + $excel_H;
                        $excel->getActiveSheet()->getRowDimension($line)->setRowHeight(24);
                        $excel->getActiveSheet()->setCellValue('A' . $line, $v['order_sn'] . " ");
                        $excel->getActiveSheet()->setCellValue('B' . $line, $v['uid']);
                        $excel->getActiveSheet()->setCellValue('C' . $line, $v['realname']);
                        $excel->getActiveSheet()->setCellValue('D' . $line, $v['tao_mobile'] . " ");
                        $excel->getActiveSheet()->setCellValue('E' . $line, $v['tao_name']);
                        $excel->getActiveSheet()->setCellValue('F' . $line, $v['address']);
                        $excel->getActiveSheet()->setCellValue('G' . $line, $v['money']);
                        $excel->getActiveSheet()->setCellValue('H' . $line, $v['level_money']);
                        $excel->getActiveSheet()->setCellValue('I' . $line, $v['order_type']);
                        $excel->getActiveSheet()->setCellValue('J' . $line, $v['tao_info']);
                        $excel->getActiveSheet()->setCellValue('K' . $line, $v['express_id']);
                        $excel->getActiveSheet()->setCellValue('L' . $line, $v['express_name']);
                        $excel->getActiveSheet()->setCellValue('M' . $line, $v['ctime']);
                        $excel->getActiveSheet()->getStyle('A' . $line . ':M' . $line)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                        //$total_money += $v['total'];
                    }

                    // 合计金额
                    /* $new_line = $line + 1;
                    $excel->getActiveSheet()->mergeCells('A' . $new_line . ':J' . $new_line);
                    $excel->getActiveSheet()->getRowDimension($new_line)->setRowHeight(24);
                    $excel->getActiveSheet()->setCellValue('A' . $new_line, '金额合计：');
                    $excel->getActiveSheet()->getStyle('A' . $new_line . ':M' . $new_line)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $excel->getActiveSheet()->getStyle('A' . $new_line)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    $excel->getActiveSheet()->setCellValue('K' . $new_line, $total_money); */

                    //设置单元格边框
                    $styleArray = array(
                        'borders' => array(
                            'allborders' => array(
                                //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                                'style' => PHPExcel_Style_Border::BORDER_THIN, //细边框
                                //'color' => array('argb' => 'FFFF0000'),
                            ),
                        ),
                    );
                    //设置单元格边框
                    $excel->getActiveSheet()->getStyle('A1:M2')->applyFromArray($styleArray);
                    //垂直居中
                    $excel->getActiveSheet()->getStyle('A1:M2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    //水平居中
                    $excel->getActiveSheet()->getStyle('A1:M2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                    //创建Excel输入对象
                    $write = new PHPExcel_Writer_Excel5($excel);
                    header("Pragma: public");
                    header("Expires: 0");
                    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
                    header("Content-Type:application/force-download");
                    header("Content-Type:application/vnd.ms-execl");
                    header("Content-Type:application/octet-stream");
                    header("Content-Type:application/download");
                    header('Content-Disposition:attachment;filename="会员开通记录' . date("Y-m-d_H:i:s", $_SERVER['REQUEST_TIME']) . '.xls"');
                    header("Content-Transfer-Encoding:binary");
                    $write->save('php://output');
                } else {
                    header("Content-type: text/html; charset=utf-8");
                    echo "没有要导出的数据";die;
                }
                // }

            }

        }

    }

    //导出资金明细
    public function fdexport()
    {
        $priv = array($this->_pvprefix . "_dex");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->display();
        }
    }

    //资金明细执行导出
    public function exportdetail()
    {
        set_time_limit(0);
        $priv  = array($this->_pvprefix . "_dex");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            if ($_GET['start_time'] != "" && $_GET['end_time'] != "") {
                $start_time = strtotime($_GET['start_time'] . ":0");
                $end_time   = strtotime($_GET['end_time'] . ":0 -1 sec");
                $event      = intval($_GET['event']);
                $keyword    = '';
                if (!empty($_GET['keyword'])) {
                    $keyword = addslashes(stripslashes(trim($_GET['keyword'])));
                }

                $data = $model->getFinanceDetailData($start_time, $end_time, $event, $keyword);

                if ($data['data']) {
                    $region = $model->getRegion();
                    foreach ($data['data'] as $k => $v) {
                        $orderData[] = array(
                            'id'           => $v['id'],
                            'realname'     => $v['realname'],
                            'name'         => $v['name'],
                            'mobile'       => $v['mobile'],
                            'recode'       => $v['recode'],
                            'create_time'  => date("Y-m-d H:i:s",$v['create_time']),
                            'event'        => $v['event'],
                            'member_level' => $v['member_level'],
                            'cmoney'       => $v['cmoney'],
                            'money'        => $v['money'],
                            'freeze'       => $v['freeze'],
                            'note'         => $v['note'],
                        );
                    }

                }

                if ($orderData) {
                    RPHP::loadClass("PHPExcel", "extend/PHPExcel/Classes");
                    $excel = new PHPExcel();

                    //设置excel属性基本信息
                    $excel->getProperties()->setCreator("YULINJUN")
                        ->setLastModifiedBy("YULINJUN")
                        ->setTitle("YULINJUN")
                        ->setSubject("用户资金明细")
                        ->setDescription("")
                        ->setKeywords("用户资金明细")
                        ->setCategory("");
                    $excel->setActiveSheetIndex(0);
                    $excel->getActiveSheet()->setTitle("用户资金明细");
                    //填入表头主标题
                    $excel->getActiveSheet()->setCellValue('A1', '用户资金明细');
                    //填入表头副标题
                    $excel->getActiveSheet()->setCellValue('A2', '导出时间范围：' . date("Y-m-d H:i:s", $start_time) . '至' . date("Y-m-d H:i:s", $end_time));
                    //合并表头单元格
                    $excel->getActiveSheet()->mergeCells('A1:M1');
                    $excel->getActiveSheet()->mergeCells('A2:M2');

                    //设置表头行高
                    $excel->getActiveSheet()->getRowDimension(1)->setRowHeight(40);
                    $excel->getActiveSheet()->getRowDimension(2)->setRowHeight(20);
                    $excel->getActiveSheet()->getRowDimension(3)->setRowHeight(30);

                    //设置表头字体
                    $excel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
                    $excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
                    $excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
                    $excel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
                    $excel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
                    $excel->getActiveSheet()->getStyle('A3:M3')->getFont()->setBold(true);
                    $letter      = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q');
                    $tableheader = array('流水号', '用户昵称', '用户姓名', '用户手机号码', '用户推荐码', '创建时间', '类型', '发生时级别', '发生金额', '可用金额', '冻结金额', '备注');
                    $excel_H     = 4;
                    for ($i = 0; $i < count($tableheader); $i++) {
                        $excel->getActiveSheet()->setCellValue("$letter[$i]3", "$tableheader[$i]");
                    }

                    $excel->getActiveSheet()->getStyle('A')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('B')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('D')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('F')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $excel->getActiveSheet()->getStyle('M')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                    $excel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('D')->setWidth(16);
                    $excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
                    $excel->getActiveSheet()->getColumnDimension('L')->setWidth(80);
                    $total_money = 0;
                    foreach ($orderData as $num => $v) {
                        $line = $num + $excel_H;
                        $excel->getActiveSheet()->getRowDimension($line)->setRowHeight(24);
                        $excel->getActiveSheet()->setCellValue('A' . $line, $v['id'] . " ");
                        $excel->getActiveSheet()->setCellValue('B' . $line, $v['realname']);
                        $excel->getActiveSheet()->setCellValue('C' . $line, $v['name']);
                        $excel->getActiveSheet()->setCellValue('D' . $line, $v['mobile'] . " ");
                        $excel->getActiveSheet()->setCellValue('E' . $line, $v['recode']);
                        $excel->getActiveSheet()->setCellValue('F' . $line, $v['create_time']);
                        $excel->getActiveSheet()->setCellValue('G' . $line, $v['event']);
                        $excel->getActiveSheet()->setCellValue('H' . $line, $v['member_level']);
                        $excel->getActiveSheet()->setCellValue('I' . $line, $v['cmoney']);
                        $excel->getActiveSheet()->setCellValue('J' . $line, $v['money']);
                        $excel->getActiveSheet()->setCellValue('K' . $line, $v['freeze']);
                        $excel->getActiveSheet()->setCellValue('L' . $line, $v['note']);
                        $excel->getActiveSheet()->getStyle('A' . $line . ':M' . $line)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                        //$total_money += $v['total'];
                    }

                    // 合计金额
                    /* $new_line = $line + 1;
                    $excel->getActiveSheet()->mergeCells('A' . $new_line . ':J' . $new_line);
                    $excel->getActiveSheet()->getRowDimension($new_line)->setRowHeight(24);
                    $excel->getActiveSheet()->setCellValue('A' . $new_line, '金额合计：');
                    $excel->getActiveSheet()->getStyle('A' . $new_line . ':M' . $new_line)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $excel->getActiveSheet()->getStyle('A' . $new_line)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    $excel->getActiveSheet()->setCellValue('K' . $new_line, $total_money); */

                    //设置单元格边框
                    $styleArray = array(
                        'borders' => array(
                            'allborders' => array(
                                //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                                'style' => PHPExcel_Style_Border::BORDER_THIN, //细边框
                                //'color' => array('argb' => 'FFFF0000'),
                            ),
                        ),
                    );
                    //设置单元格边框
                    $excel->getActiveSheet()->getStyle('A1:M2')->applyFromArray($styleArray);
                    //垂直居中
                    $excel->getActiveSheet()->getStyle('A1:M2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    //水平居中
                    $excel->getActiveSheet()->getStyle('A1:M2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                    //创建Excel输入对象
                    $write = new PHPExcel_Writer_Excel5($excel);
                    header("Pragma: public");
                    header("Expires: 0");
                    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
                    header("Content-Type:application/force-download");
                    header("Content-Type:application/vnd.ms-execl");
                    header("Content-Type:application/octet-stream");
                    header("Content-Type:application/download");
                    header('Content-Disposition:attachment;filename="用户资金明细' . date("Y-m-d_H:i:s", $_SERVER['REQUEST_TIME']) . '.xls"');
                    header("Content-Transfer-Encoding:binary");
                    $write->save('php://output');
                } else {
                    header("Content-type: text/html; charset=utf-8");
                    echo "没有要导出的数据";die;
                }
                // }

            }

        }
    }
}
